package com.example.swaggerDemo.controller;


import com.example.swaggerDemo.entity.MsgEntity;
import com.example.swaggerDemo.entity.SalaryEntity;
import com.example.swaggerDemo.entity.SysUserEntity;
import com.example.swaggerDemo.service.SalaryService;
import com.example.swaggerDemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.util.List;

@Api("财务信息服务")
@RestController
@RequestMapping(value="/Salary")
public class SalaryController {
    @Autowired
    private UserService SysUserDao;

    @Autowired
    private SalaryService Service;


    @ApiOperation(value="按用户id查询财务情况按状态:0=未申请,1=待付款,2=已付款",notes="查询财务情况按状态:0=未申请,1=待付款,2=已付款")
    @ResponseBody
    @RequestMapping(value="/findByupopenid",method=RequestMethod.POST)
    public Object findByupopenid(@RequestParam("userid") String userid, @RequestParam("status") String status, @RequestParam("pageindex") int pagindex,
                                 @RequestParam("pagesize") int pagesize) {
        MsgEntity msg=new MsgEntity();
        try {

            List<SalaryEntity> dto = Service.findOpenid(userid,status,pagindex,pagesize);
            if ( dto.size()>0){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (Exception e) {
            msg.msg="没有查询导数据";
            msg.code=false;
            msg.object=null;
        }
        return msg;
    }



    @ApiOperation(value="添加成为经纪人的财务信息",notes="添加成为经纪人的财务信息")
    @ResponseBody
    @RequestMapping(value="/insert",method= RequestMethod.POST)
    public  Object insert(@RequestParam("userid") String userid, @RequestParam("money") String money) throws SQLException {
        int dto = 0;
        MsgEntity msg = new MsgEntity();
        SysUserEntity user = SysUserDao.findByid(userid);

        dto = Service.insert(user.getOpenid(), money);
        if (dto > 0) {
            msg.msg = "添加成功";
            msg.code = true;
            msg.object = dto;
        } else {
            msg.msg = "添加失败";
            msg.code = false;
            msg.object = dto;
        }
        return msg;
    }


    @ApiOperation(value="修改订单状态",notes="修改订单状态")
    @ResponseBody
    @RequestMapping(value="/updateOrderType",method= RequestMethod.POST)
    public  Object updateOrderType(@RequestParam("id") String id, @RequestParam("status") Integer status){
        MsgEntity msg=new MsgEntity();
        int dto = Service.updateOrderType(id,status);
        if ( dto>0){
            msg.msg="添加成功";
            msg.code=true;
            msg.object=dto;
        }
        else
        {
            msg.msg="添加失败";
            msg.code=false;
            msg.object=dto;
        }
        return msg;
    }

    @ApiOperation(value="添加提现信息",notes="添加提现信息")
    @ResponseBody
    @RequestMapping(value="/insertTx",method= RequestMethod.POST)
    public  Object insertTx(@RequestParam("userid") String userid,
                            @RequestParam("money") String money,
                            @RequestParam("kaihuhang") String kaihuhang,
                            @RequestParam("yinhangka") String yinhangka,
                            @RequestParam("username") String username,
                            @RequestParam("remake") String remake) throws SQLException {
        int dto = 0;
        MsgEntity msg = new MsgEntity();
        SysUserEntity user = SysUserDao.findByid(userid);
        if (user.getMoney()-Double.parseDouble(money)<0)
        {
            msg.msg = "提现失败账号余额不够！";
            msg.code = false;
            msg.object = null;
        }
        else {
            String type22money =(String.format("%.1f",Double.parseDouble(money)));
            dto = Service.insertTx(userid, type22money, kaihuhang, yinhangka, remake,username,user.getAvatar());
            if (dto > 0) {
                String[] strarray = remake.split(",");
                for (int i = 0; i < strarray.length; i++) {
                    Service.updateOrderTypeUpid(strarray[i], 1,dto,user.getAvatar());//写入关联提现id
                }
                double k = user.getMoney() - Double.parseDouble(type22money);
                SysUserDao.updateMoney(k, userid);
                msg.msg = "添加成功";
                msg.code = true;
                msg.object = dto;
            } else {
                msg.msg = "添加失败";
                msg.code = false;
                msg.object = dto;
            }
        }
        return msg;
    }


    @ApiOperation(value="管理员查询佣金信息",notes="管理员查询佣金信息")
    @ResponseBody
    @RequestMapping(value="/findadmin",method= RequestMethod.POST)
    public  Object findadmin(@RequestParam("status") String status,
                             @RequestParam("pageindex") int pagindex,
                             @RequestParam("pagesize") int pagesize) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
            List<SalaryEntity> dto = Service.findadmin(status,pagindex,pagesize);
            if ( dto.size()>0){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="管理员查询佣金提现明细",notes="管理员查询佣金提现明细")
    @ResponseBody
    @RequestMapping(value="/findByupid",method= RequestMethod.POST)
    public  Object findByupid(@RequestParam("upid") String upid) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
            List<SalaryEntity> dto = Service.findByupid(upid);
            if ( dto.size()>0){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }


    @ApiOperation(value="管理员查询佣金提现合计",notes="管理员查询佣金提现合计")
    @ResponseBody
    @RequestMapping(value="/findByid",method= RequestMethod.POST)
    public  Object findByid(@RequestParam("id") String id) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
            SalaryEntity dto = Service.findByid(id);
            if ( dto!=null){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @ApiOperation(value="查询每一个订单的佣金",notes="查询每一个订单的佣金")
    @ResponseBody
    @RequestMapping(value="/findByUserid",method= RequestMethod.POST)
    public  Object findByUserid(@RequestParam("userid") String userid,@RequestParam("orderid") String orderid,@RequestParam("type") String type) throws SQLException
    {
        MsgEntity msg=new MsgEntity();
        try {
            List<SalaryEntity> dto = Service.findByUserid(orderid);
            if ( dto!=null){
                msg.msg="数据查询成功";
                msg.code=true;
                msg.object=dto;
            }
            else
            {
                msg.msg="没有查询导数据";
                msg.code=false;
                msg.object=dto;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;
    }
}
